package com.copera.cn.kaka;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

import org.json.JSONObject;

import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.copera.cn.kaka.util.MyBarAndQrcodeUtils;
import com.zxing.encoding.EncodingHandler;

public class ShopCodeActivity extends BaseActivity {

	private RelativeLayout back;
	private ImageView qrImgImageView;
	private Button download;
	private TextView textView1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.shop_code);
		back = (RelativeLayout) findViewById(R.id.back);
		download = (Button) findViewById(R.id.loginButton);
		qrImgImageView = (ImageView) this.findViewById(R.id.imageView1);
		textView1 = (TextView)findViewById(R.id.textView1);
		textView1.setText(getAppCustomer().getShop().getShopName()+"的店铺二维码");
		back.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();

			}
		});
		download.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				File file = new File("/sdcard/kaka/");

				if (!file.exists()) {
					file.mkdirs();
				}
				File imageFile = new File(file, new Date().getTime() + ".png");

				try {
					imageFile.createNewFile();
					FileOutputStream fos = new FileOutputStream(imageFile);
					qrImgImageView.setDrawingCacheEnabled(true);
					Bitmap obmp = Bitmap.createBitmap(qrImgImageView
							.getDrawingCache());
					qrImgImageView.setDrawingCacheEnabled(false);
					obmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
					fos.flush();
					fos.close();
					Toast.makeText(ShopCodeActivity.this,
							"二维码图片已保存到" + imageFile, Toast.LENGTH_LONG).show();
					Log.i("1", "meicuo");

				} catch (IOException e) {
					// TODO Auto-generated catch block
					Log.i("1", "catch");
					e.printStackTrace();
				}

			}
		});
		//强制为竖屏 
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	}

	@Override
	protected void onResume() {
		loadTwoCode();
		super.onResume();
	}

	public void loadTwoCode() {

		new AsyncTask<String, Integer, JSONObject>() {

			@Override
			protected JSONObject doInBackground(String... params) {
				JSONObject jsonObj = null;

				return jsonObj;
			}

			@Override
			protected void onPostExecute(JSONObject result) {
				Bitmap logo = null;
				MyBarAndQrcodeUtils mycode = new MyBarAndQrcodeUtils();
				try {
					Bitmap qrCodeBitmap = EncodingHandler.createQRCode(
							getAppCustomer().getShop().getShopId(), 900);

					logo = BitmapFactory.decodeResource(getResources(),
							R.drawable.logo_kaka);
					Bitmap bmp = mycode.overlayLogo(qrCodeBitmap, logo);
					qrImgImageView.setImageBitmap(bmp);

				} catch (Exception e) {

					Log.i("1", "catch");
					e.printStackTrace();
				}

			}

		}.execute("");

	}

}
